package com.sense.cloud.demo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sense.cloud.common.API2Request;
import sun.reflect.generics.tree.VoidDescriptor;

import java.io.IOException;
import java.net.URISyntaxException;
import java.security.CryptoPrimitive;
import java.security.InvalidKeyException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;

/**
 * Created by local.kanghl on 2018/8/2.
 */
public class OpenAPIDemoByJava {

    // Virbox LM 平台 OpenAPI V2 应用基地址
    private static String appUrl = "https://openapi.senseyun.com";
    // 使用时应换成自己的 appId 和 appSecret.
    // 如果查看 appId/appSecret 请查看文档：https://developer.lm.virbox.com/course/course.html#other
    private static String appId = "0b0c24be2c614fa48c2067aedc43ec71";
    private static String appSecret = "1432c2b485104a9ebecd2019703613e3";
    // 初始化 api 请求
    private static API2Request api2Request = new API2Request(appUrl, appId, appSecret);

    // 声明测试过程中的中间变量，因为某些接口的参数依赖指定接口的返回数据
    // 普通用户的 userId
    private static String userid = null;
    // 影子账号 account
    private static String shadowAccountWithDeveloperGuid = null;
    // 影子账号的 userId
    private static String shadowAccountUserId = null;
    // 软许可模板 id
    private static String tidofsoft = null;
    // 云许可模板 id
    private static String tidofcloud = null;
    // 软许可的唯一 guid
    private static String licenseGuid = null;
    // 标签 id
    private static String tagid = null;
    // 声明中间变量结束


    // 获取当前时间戳
    private static long  now = Calendar.getInstance().getTime().getTime()/1000;

    // 声明测试过程中用到的一些数据
    // 截取当前时间，用来模拟手机号，手机号必须是1开头的11位数字。
    // 正式使用时请使用正确的手机号，否则会影响业务使用
    private static String userAccount = "1" + now;
    // 影子账号
    private static String shadowAccount = "OpenAPIV2-" + now;
    // 产品id, 在此测试使用固定的 id.
    private static int licenseId = 17;
    // 声明测试数据结束

    public static void main(String[] args) throws KeyManagementException, IOException, NoSuchAlgorithmException, InvalidKeyException, URISyntaxException {
        /*addUser();
        findUser();
        modifyUserPassword();
        modifyUser();
        addShadowUser();
        modifyShadowUserPassword();


        addProduct();
        modifyProduct();
        getProductInfo();
        productList();

        addTemplateOfCloud();
        addTemplateOfSoft();
        modifyTemplate();
        templateList();
        findTemplate();

        issueLicense();
        issueLicenseToShadowAccount();
        issueLicenseByProduct();
        licenseList();
        updateLicense();
        licenseUseInfo();
        softLockbindList();
        softLockbindHistory();


        addTag();
        updateTag();
        findTags();
        deleteTag();

        deleteTemplateOfCloud();
        deleteTemplateOfSoft();
        deleteProduct();
        deleteShadowUser();
        deleteUser();*/

        //ddlic();
        //updatelic();
        //trllic();
        dellic();;
        System.out.println("执行完成");
    }

    // 测试普通账号开始

    /// <summary>
    /// 添加普通用户
    /// </summary>
    private static void addUser() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {

        JSONObject userJson = new JSONObject();
        userJson.put("userAccount",userAccount );
        userJson.put("nickname", "kanghl");
        userJson.put("passwd","123456" );
        userJson.put("accountType",2 );
        JSONObject resultJson = api2Request.send("/v2/sv/addUser", userJson);
        userid = resultJson.getJSONObject("data").getString("userId");
    }

    /// <summary>
    /// 查找用户
    /// </summary>
    private static void findUser() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject userJson = new JSONObject();
        userJson.put("cellphone",userAccount);
        JSONObject resultJson = api2Request.send("/v2/sv/findUsers", userJson);

    }

    /// <summary>
    /// 修改用户密码，因为只能修改影子账号的密码，所以普通用户的密码无法修改
    /// </summary>
    private static void modifyUserPassword() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject userJson = new JSONObject();
        userJson.put("userAccount",userAccount);
        userJson.put("passwd","12345678" );

        JSONObject resultJson = api2Request.send("/v2/sv/modifyUserPasswd", userJson);
    }

    /// <summary>
    /// 修改用户信息
    /// </summary>
    private static void modifyUser() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject userJson = new JSONObject();
        userJson.put("userId", userid );
        userJson.put("desc","修改用户信息");
        JSONObject resultJson = api2Request.send("/v2/sv/modifyUser", userJson);
    }

    // 测试普通账号结束

    // 测试影子账号开始

    private static void addShadowUser() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject userJson = new JSONObject();
        userJson.put("shadowAccount",shadowAccount );
        userJson.put("nickname","kanghl" );
        userJson.put("passwd","123456" );

        JSONObject resultJson = api2Request.send("/v2/sv/addShadowUser", userJson);
        shadowAccountWithDeveloperGuid = resultJson.getJSONObject("data").getString("userAccount");
        shadowAccountUserId = resultJson.getJSONObject("data").getString("userId");
    }
    /// <summary>
    /// 修改影子账号密码
    /// </summary>
    private static void modifyShadowUserPassword() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject userJson = new JSONObject();
        userJson.put("userAccount",shadowAccountWithDeveloperGuid );
        userJson.put("passwd","12345678" );

        JSONObject resultJson = api2Request.send("/v2/sv/modifyUserPasswd", userJson);
    }

    // 测试影子账号结束

    // 测试产品开始

    private static void addProduct() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject productJson = new JSONObject();
        productJson.put("licenseId",licenseId);
        productJson.put("productName","测试名称API2.0");

        JSONArray licenseFormArr = new JSONArray();
        licenseFormArr.add(1);
        licenseFormArr.add(2);

        productJson.put("licenseForm",licenseFormArr);
        productJson.put("pub","1234" );
        productJson.put("rom","123456" );
        productJson.put("raw","12345678" );

        //定义模块
        JSONObject module1= new JSONObject();
        module1.put("moduleId",1);
        module1.put("moduleName","高级功能1" );
        JSONObject module2= new JSONObject();
        module2.put("moduleId",2);
        module2.put("moduleName","高级功能2" );

        JSONArray moduleArr = new JSONArray();
        moduleArr.add(module1);
        moduleArr.add(module2);
        //添加模块
        productJson.put("modules",moduleArr);

        JSONObject resultJson = api2Request.send("/v2/sv/addProduct", productJson);
        
    }

    private static void modifyProduct() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {

        JSONObject productJson = new JSONObject();
        productJson.put("licenseId",licenseId );
        productJson.put("productName","修改称");
        JSONObject resultJson = api2Request.send("/v2/sv/modifyProduct", productJson);
    }

    private static void getProductInfo() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject productJson = new JSONObject();
        productJson.put("licenseId",licenseId );
        JSONObject resultJson = api2Request.send("/v2/sv/getProductInfo", productJson);
    }

    private static void productList() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject productJson = new JSONObject();
        productJson.put("productName","API2" );
        JSONObject resultJson = api2Request.send("/v2/sv/productList", productJson);
    }

    // 测试产品结束

    // 测试模板开始

    /// <summary>
    /// 添加云锁模板
    /// </summary>
    private static void addTemplateOfCloud() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject templateJson = new JSONObject();
        templateJson.put("templateName","测试API2.0-云锁" );
        templateJson.put("licenseId",licenseId );
        templateJson.put("licenseForm",1 );
        templateJson.put("isForever",true );
        templateJson.put("canModify",1 );

        JSONObject module1= new JSONObject();
        module1.put("moduleId",1);
        module1.put("moduleName","高级功能1" );

        JSONArray moduleArr = new JSONArray();
        moduleArr.add(module1);
        templateJson.put("module",moduleArr);

        JSONObject resultJson = api2Request.send("/v2/sv/addTemplate", templateJson);
        tidofcloud = resultJson.getJSONObject("data").getString("templateId");
    }


    private static void addTemplateOfSoft() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject templateJson = new JSONObject();
        templateJson.put("templateName","测试API2.0-软锁" );
        templateJson.put("licenseId",licenseId );
        templateJson.put("licenseForm",2 );
        templateJson.put("isForever",false );
        templateJson.put("licStartTimeType",0 );
        templateJson.put("startTime", now);
        templateJson.put("endTime",now+1000);
        templateJson.put("offlineDays",2 );
        templateJson.put("licBindLimit",2 );
        templateJson.put("licBindMaxLimit",2 );
        templateJson.put("licenseMode",0 );
        templateJson.put("pricingMode","yearly" );
        templateJson.put("canModify",1 );

        JSONObject module1= new JSONObject();
        module1.put("moduleId",1);
        module1.put("moduleName","高级功能1" );

        JSONArray moduleArr = new JSONArray();
        moduleArr.add(module1);
        templateJson.put("module",moduleArr);
        JSONObject resultJson = api2Request.send("/v2/sv/addTemplate", templateJson);
        tidofsoft = resultJson.getJSONObject("data").getString("templateId");
    }

    private static void modifyTemplate() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject templateJson = new JSONObject();
        templateJson.put("templateId",tidofsoft );
        templateJson.put("templateName","测试API2.0-软锁-修改名称" );
        templateJson.put("licenseForm",2 );
        templateJson.put("isForever",true );
        templateJson.put("offlineDays",2 );
        templateJson.put("licBindLimit",2 );
        templateJson.put("licBindMaxLimit",2 );
        templateJson.put("licenseMode",0);
        templateJson.put("pricingMode","once" );
        templateJson.put("canModify",1 );
        JSONObject resultJson = api2Request.send("/v2/sv/modifyTemplate", templateJson);
    }

    private static void templateList() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject templateJson = new JSONObject();
        templateJson.put("licenseId",licenseId );
        JSONObject resultJson = api2Request.send("/v2/sv/templateList", templateJson);
    }

    private static void findTemplate() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject templateJson = new JSONObject();
        templateJson.put("templateId",tidofsoft );
        JSONObject resultJson = api2Request.send("/v2/sv/findTemplate", templateJson);
    }

    // 测试模板结束

    // 测试许可开始

    private static void issueLicense() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject licenseJson = new JSONObject();
        licenseJson.put("templateId",tidofcloud );
        licenseJson.put("userAccount",userAccount );
        JSONObject resultJson = api2Request.send("/v2/sv/issueLicense", licenseJson);
    }

    private static void issueLicenseToShadowAccount() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject licenseJson = new JSONObject();
        licenseJson.put("templateId",tidofcloud );
        licenseJson.put("userAccount",shadowAccountWithDeveloperGuid );
        JSONObject resultJson = api2Request.send("/v2/sv/issueLicense", licenseJson);
    }

    private static void issueLicenseByProduct() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject licenseJson = new JSONObject();
        licenseJson.put("licenseId",licenseId );
        licenseJson.put("licenseForm",2 );
        licenseJson.put("isForever",false );
        licenseJson.put("userAccount", userAccount );
        licenseJson.put("version",2 );
        licenseJson.put("startTime", now);
        licenseJson.put("endTime",now+1000 );
        licenseJson.put("span",3 );
        licenseJson.put("offlineDays",3 );
        licenseJson.put("licBindLimit",3 );
        licenseJson.put("licBindMaxLimit",3 );
        licenseJson.put("isOffline",true );
        licenseJson.put("licenseMode",0 );
        licenseJson.put("pricingMode","yearly" );
        JSONObject resultJson = api2Request.send("/v2/sv/issueLicenseByProduct", licenseJson);
    }

    private static void licenseList() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject licenseJson = new JSONObject();

        JSONArray licenseFormArr = new JSONArray();
        licenseFormArr.add(2);
        licenseJson.put("licenseForms",licenseFormArr);
        licenseJson.put("userAccount",userAccount);
        JSONObject resultJson = api2Request.send("/v2/sv/licenseList", licenseJson);
        licenseGuid = resultJson.getJSONObject("data").getJSONArray("licenses").getJSONObject(0).getString("pubLicenseGuid");
    }

    private static void updateLicense() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject licenseJson = new JSONObject();
        {
            licenseJson.put("licenseId",licenseId );
            licenseJson.put("licenseForm",2 );
            licenseJson.put("userAccount", userAccount );

            JSONObject versionJson = new JSONObject();
            versionJson.put("op","set");
            versionJson.put("value",4);
            licenseJson.put("version",versionJson);

            JSONObject spanJson = new JSONObject();
            spanJson.put("op","set");
            spanJson.put("value",4);
            licenseJson.put("span",spanJson);

        };
        JSONObject resultJson = api2Request.send("/v2/sv/updateLicense", licenseJson);
    }

    private static void licenseUseInfo() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject licenseJson = new JSONObject();
        licenseJson.put("licenseId",licenseId );
        licenseJson.put("licenseForm",2 );
        licenseJson.put("userAccount", userAccount );

        JSONObject resultJson = api2Request.send("/v2/sv/licenseUseInfo", licenseJson);
    }

    private static void softLockbindList() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject licenseJson = new JSONObject();
        licenseJson.put("licenseGuid",licenseGuid );
        JSONObject resultJson = api2Request.send("/v2/sv/softLockbindList", licenseJson);
    }

    private static void softLockbindHistory() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject licenseJson = new JSONObject();
        licenseJson.put("licenseGuid",licenseGuid );
        JSONObject resultJson = api2Request.send("/v2/sv/softLockbindHistory", licenseJson);
    }


    /**
     * 签发添加许可升级包
     */
    private static void addlic() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject licJson = new JSONObject();
        licJson.put("pin","1qaz2wsx3edc4rfv");
        licJson.put("deviceId","9733C8010007020510610004000D0017");
        licJson.put("shellNumber","099900000039");
        licJson.put("deviceCert","MIIDHDCCAgSgAwIBAgICWTowDQYJKoZIhvcNAQEFBQAwFDESMBAGA1UEAxMJRGV2aWNlIENBMB4XDTE2MDEwNzA3MDczMVoXDTM2MDEwMjA3MDczMVowgY0xCzAJBgNVBAYTAkNOMRAwDgYDVQQIDAdCZWlqaW5nMRAwDgYDVQQHDAdCZWlqaW5nMRIwEAYDVQQKDAlTZW5zZWxvY2sxEjAQBgNVBAsMCVNlbnNlbG9jazEyMDAGA1UEAwwpREVWSUNFSUQtOTczM0M4MDEwMDA3MDIwNTEwNjEwMDA0MDAwRDAwMTcwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCY+BpKeuhlen+jk1NSs1sdfJXBqGtIxhFdMf/73wfRspmF7HI469JFOumo3wInwDPs4sX5jx2/ewOfgw1wAqnmuy677zqN5Flip04touc1qDqx1XajBTy0W1O3ERFS81BNSyblzrFwpzI6vLcjh5xZf5iAPQN8PPRv71XkkG1kgJT1Ekr1BbIiQn29M+tnooRt9OhAe3hQOSu82Llrc0SOSTFAuabFzP8jEn0TeJjxC8J0td+pqa8Yz5VumANqETtPU/RvvcYpLlIUY7iwB1tdIyG4qcC0ZEGEhS5+EEJpG5sVRUzeTJpoZERTzMVS8ud6Dp/1ShIDShWTPBrJv637AgMBAAEwDQYJKoZIhvcNAQEFBQADggEBACzxRH9whSI26EZ5KSMud40PCqtogNj6kgtYSPx11UFHBSLiefqGtZkqGavHrrLdNP+8bEbQgSWnGxyOfsK+0FWZ/5C7Yzn9UfuGGOBhdI7sexr/bkMEkeIyy9D/La0pe4W1LJXD38Z+9tycg4bddGCO/PShuqsrV0VX8fBDI6U7zd8vD9eJpY8O4ppJ9+tPCcH1utYkj9V8nz7mDCg3mrZp+SkqvcUnBvXDJkYU4cF1WjqZRJFMDfZJ5n4I59Ai2IqJMoKZCcERK9blbzCjO4OF+yxCO+MNqiGtilrftcor5zmO253vdyO1EzJG4Un0BdjnTR52cIfUNVUbx+qtbNk=");
        licJson.put("licenseId",licenseId);
        licJson.put("force",true);
        licJson.put("isForever",false);
        licJson.put("startTime",now);
        licJson.put("endTime",now+100000);
        licJson.put("counter",3);
        licJson.put("span",3);
        licJson.put("concurrent",10);
        licJson.put("concurrentType","process");

        JSONArray moduleArr = new JSONArray();
        moduleArr.add(1);
        licJson.put("module",moduleArr);

        JSONObject romJson = new JSONObject();
        romJson.put("data","1234");
        licJson.put("rom",romJson);

        JSONObject rawJson = new JSONObject();
        rawJson.put("data","123456");
        rawJson.put("offset",0);
        rawJson.put("resize",3);
        licJson.put("raw",rawJson);

        JSONObject pubJson = new JSONObject();
        pubJson.put("data","123456");
        pubJson.put("offset",0);
        pubJson.put("resize",6);
        licJson.put("pub",pubJson);

        licJson.put("licDesc","添加硬件许可");

        api2Request.send("/v2/sv/devicelicense/addlic",licJson);

    }

    /**
     * 签发更新许可升级包
     */
    private static void updatelic() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject licJson = new JSONObject();
        licJson.put("pin","1qaz2wsx3edc4rfv");
        licJson.put("deviceId","9733C8010007020510610004000D0017");
        licJson.put("shellNumber","099900000039");
        licJson.put("deviceCert","MIIDHDCCAgSgAwIBAgICWTowDQYJKoZIhvcNAQEFBQAwFDESMBAGA1UEAxMJRGV2aWNlIENBMB4XDTE2MDEwNzA3MDczMVoXDTM2MDEwMjA3MDczMVowgY0xCzAJBgNVBAYTAkNOMRAwDgYDVQQIDAdCZWlqaW5nMRAwDgYDVQQHDAdCZWlqaW5nMRIwEAYDVQQKDAlTZW5zZWxvY2sxEjAQBgNVBAsMCVNlbnNlbG9jazEyMDAGA1UEAwwpREVWSUNFSUQtOTczM0M4MDEwMDA3MDIwNTEwNjEwMDA0MDAwRDAwMTcwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCY+BpKeuhlen+jk1NSs1sdfJXBqGtIxhFdMf/73wfRspmF7HI469JFOumo3wInwDPs4sX5jx2/ewOfgw1wAqnmuy677zqN5Flip04touc1qDqx1XajBTy0W1O3ERFS81BNSyblzrFwpzI6vLcjh5xZf5iAPQN8PPRv71XkkG1kgJT1Ekr1BbIiQn29M+tnooRt9OhAe3hQOSu82Llrc0SOSTFAuabFzP8jEn0TeJjxC8J0td+pqa8Yz5VumANqETtPU/RvvcYpLlIUY7iwB1tdIyG4qcC0ZEGEhS5+EEJpG5sVRUzeTJpoZERTzMVS8ud6Dp/1ShIDShWTPBrJv637AgMBAAEwDQYJKoZIhvcNAQEFBQADggEBACzxRH9whSI26EZ5KSMud40PCqtogNj6kgtYSPx11UFHBSLiefqGtZkqGavHrrLdNP+8bEbQgSWnGxyOfsK+0FWZ/5C7Yzn9UfuGGOBhdI7sexr/bkMEkeIyy9D/La0pe4W1LJXD38Z+9tycg4bddGCO/PShuqsrV0VX8fBDI6U7zd8vD9eJpY8O4ppJ9+tPCcH1utYkj9V8nz7mDCg3mrZp+SkqvcUnBvXDJkYU4cF1WjqZRJFMDfZJ5n4I59Ai2IqJMoKZCcERK9blbzCjO4OF+yxCO+MNqiGtilrftcor5zmO253vdyO1EzJG4Un0BdjnTR52cIfUNVUbx+qtbNk=");
        licJson.put("licenseId",licenseId);
        licJson.put("force",true);
        licJson.put("isForever",false);

        JSONObject startTimeJson = new JSONObject();
        startTimeJson.put("op","set");
        startTimeJson.put("value",now);
        licJson.put("startTime",startTimeJson);

        JSONObject endTimeJson = new JSONObject();
        endTimeJson.put("op","inc");
        endTimeJson.put("value",100);
        licJson.put("endTime",endTimeJson);

        JSONObject counterJson = new JSONObject();
        counterJson.put("op","dec");
        counterJson.put("value","1");
        licJson.put("counter",counterJson);

        JSONObject spanJson = new JSONObject();
        spanJson.put("op","disable");
        licJson.put("span",spanJson);

        licJson.put("concurrent",10);
        licJson.put("concurrentType","process");

        JSONArray moduleArr = new JSONArray();
        moduleArr.add(1);
        licJson.put("module",moduleArr);

        JSONObject romJson = new JSONObject();
        romJson.put("data","1234");
        licJson.put("rom",romJson);

        JSONObject rawJson = new JSONObject();
        rawJson.put("data","123456");
        rawJson.put("offset",0);
        rawJson.put("resize",3);
        licJson.put("raw",rawJson);

        JSONObject pubJson = new JSONObject();
        pubJson.put("data","123456");
        pubJson.put("offset",0);
        pubJson.put("resize",6);
        licJson.put("pub",pubJson);

        licJson.put("licDesc","更新硬件许可");

        api2Request.send("/v2/sv/devicelicense/updatelic",licJson);

    }

    /**
     * 签发控制许可升级包
     */
    private static void ctrllic() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject licJson = new JSONObject();
        licJson.put("pin","1qaz2wsx3edc4rfv");
        licJson.put("deviceId","9733C8010007020510610004000D0017");
        licJson.put("shellNumber","099900000039");
        licJson.put("deviceCert","MIIDHDCCAgSgAwIBAgICWTowDQYJKoZIhvcNAQEFBQAwFDESMBAGA1UEAxMJRGV2aWNlIENBMB4XDTE2MDEwNzA3MDczMVoXDTM2MDEwMjA3MDczMVowgY0xCzAJBgNVBAYTAkNOMRAwDgYDVQQIDAdCZWlqaW5nMRAwDgYDVQQHDAdCZWlqaW5nMRIwEAYDVQQKDAlTZW5zZWxvY2sxEjAQBgNVBAsMCVNlbnNlbG9jazEyMDAGA1UEAwwpREVWSUNFSUQtOTczM0M4MDEwMDA3MDIwNTEwNjEwMDA0MDAwRDAwMTcwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCY+BpKeuhlen+jk1NSs1sdfJXBqGtIxhFdMf/73wfRspmF7HI469JFOumo3wInwDPs4sX5jx2/ewOfgw1wAqnmuy677zqN5Flip04touc1qDqx1XajBTy0W1O3ERFS81BNSyblzrFwpzI6vLcjh5xZf5iAPQN8PPRv71XkkG1kgJT1Ekr1BbIiQn29M+tnooRt9OhAe3hQOSu82Llrc0SOSTFAuabFzP8jEn0TeJjxC8J0td+pqa8Yz5VumANqETtPU/RvvcYpLlIUY7iwB1tdIyG4qcC0ZEGEhS5+EEJpG5sVRUzeTJpoZERTzMVS8ud6Dp/1ShIDShWTPBrJv637AgMBAAEwDQYJKoZIhvcNAQEFBQADggEBACzxRH9whSI26EZ5KSMud40PCqtogNj6kgtYSPx11UFHBSLiefqGtZkqGavHrrLdNP+8bEbQgSWnGxyOfsK+0FWZ/5C7Yzn9UfuGGOBhdI7sexr/bkMEkeIyy9D/La0pe4W1LJXD38Z+9tycg4bddGCO/PShuqsrV0VX8fBDI6U7zd8vD9eJpY8O4ppJ9+tPCcH1utYkj9V8nz7mDCg3mrZp+SkqvcUnBvXDJkYU4cF1WjqZRJFMDfZJ5n4I59Ai2IqJMoKZCcERK9blbzCjO4OF+yxCO+MNqiGtilrftcor5zmO253vdyO1EzJG4Un0BdjnTR52cIfUNVUbx+qtbNk=");

        licJson.put("ctrlType",3);
        licJson.put("startTime",now);
        licJson.put("endTime",now+100000);
        licJson.put("licDesc","控制许可");

        api2Request.send("/v2/sv/devicelicense/ctrllic",licJson);
    }

    /**
     * 删除硬件许可升级包
     */
    private static void dellic() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject licJson = new JSONObject();
        licJson.put("pin","1qaz2wsx3edc4rfv");
        licJson.put("deviceId","9733C8010007020510610004000D0017");
        licJson.put("shellNumber","099900000039");
        licJson.put("deviceCert","MIIDHDCCAgSgAwIBAgICWTowDQYJKoZIhvcNAQEFBQAwFDESMBAGA1UEAxMJRGV2aWNlIENBMB4XDTE2MDEwNzA3MDczMVoXDTM2MDEwMjA3MDczMVowgY0xCzAJBgNVBAYTAkNOMRAwDgYDVQQIDAdCZWlqaW5nMRAwDgYDVQQHDAdCZWlqaW5nMRIwEAYDVQQKDAlTZW5zZWxvY2sxEjAQBgNVBAsMCVNlbnNlbG9jazEyMDAGA1UEAwwpREVWSUNFSUQtOTczM0M4MDEwMDA3MDIwNTEwNjEwMDA0MDAwRDAwMTcwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCY+BpKeuhlen+jk1NSs1sdfJXBqGtIxhFdMf/73wfRspmF7HI469JFOumo3wInwDPs4sX5jx2/ewOfgw1wAqnmuy677zqN5Flip04touc1qDqx1XajBTy0W1O3ERFS81BNSyblzrFwpzI6vLcjh5xZf5iAPQN8PPRv71XkkG1kgJT1Ekr1BbIiQn29M+tnooRt9OhAe3hQOSu82Llrc0SOSTFAuabFzP8jEn0TeJjxC8J0td+pqa8Yz5VumANqETtPU/RvvcYpLlIUY7iwB1tdIyG4qcC0ZEGEhS5+EEJpG5sVRUzeTJpoZERTzMVS8ud6Dp/1ShIDShWTPBrJv637AgMBAAEwDQYJKoZIhvcNAQEFBQADggEBACzxRH9whSI26EZ5KSMud40PCqtogNj6kgtYSPx11UFHBSLiefqGtZkqGavHrrLdNP+8bEbQgSWnGxyOfsK+0FWZ/5C7Yzn9UfuGGOBhdI7sexr/bkMEkeIyy9D/La0pe4W1LJXD38Z+9tycg4bddGCO/PShuqsrV0VX8fBDI6U7zd8vD9eJpY8O4ppJ9+tPCcH1utYkj9V8nz7mDCg3mrZp+SkqvcUnBvXDJkYU4cF1WjqZRJFMDfZJ5n4I59Ai2IqJMoKZCcERK9blbzCjO4OF+yxCO+MNqiGtilrftcor5zmO253vdyO1EzJG4Un0BdjnTR52cIfUNVUbx+qtbNk=");

        licJson.put("licenseId",licenseId);
        licJson.put("force",true);
        licJson.put("licDesc","控制许可");

        api2Request.send("/v2/sv/devicelicense/dellic",licJson);

    }

    // 测试许可结束

    // 测试标签开始

    private static void addTag() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject tagJson = new JSONObject();
        tagJson.put("name","测试标签" );
        JSONObject resultJson = api2Request.send("/v2/sv/addTag", tagJson);
        tagid = resultJson.getJSONObject("data").getString("id");
    }

    private static void updateTag() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject tagJson = new JSONObject();
        tagJson.put("name","测试标签修改" );
        tagJson.put("id",tagid );
        JSONObject resultJson = api2Request.send("/v2/sv/updateTag", tagJson);
    }

    private static void findTags() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject tagJson = new JSONObject();
        tagJson.put("name","测试" );
        JSONObject resultJson = api2Request.send("/v2/sv/findTags", tagJson);
    }

    private static void deleteTag() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject tagJson = new JSONObject();
        tagJson.put("id",tagid);
        JSONObject resultJson = api2Request.send("/v2/sv/deleteTag", tagJson);
    }
    // 测试标签结束


    // 开始清理数据
    // 主要清理用户、产品、模板

    private static void deleteTemplateOfCloud() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject templateJson = new JSONObject();
        templateJson.put("templateId",tidofcloud );

        JSONObject resultJson = api2Request.send("/v2/sv/deleteTemplate", templateJson);
    }
    private static void deleteTemplateOfSoft() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject templateJson = new JSONObject();
        templateJson.put("templateId",tidofsoft );

        JSONObject resultJson = api2Request.send("/v2/sv/deleteTemplate", templateJson);
    }

    private static void deleteProduct() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject productJson = new JSONObject();
        productJson.put("licenseId",licenseId );

        JSONObject resultJson = api2Request.send("/v2/sv/deleteProduct", productJson);
    }

    private static void deleteShadowUser() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject userJson = new JSONObject();
        userJson.put("userId",shadowAccountUserId );

        JSONObject resultJson = api2Request.send("/v2/sv/deleteUser", userJson);
    }
    private static void deleteUser() throws URISyntaxException, IOException, NoSuchAlgorithmException, InvalidKeyException, KeyManagementException {
        JSONObject userJson = new JSONObject();
        userJson.put("userId",userid );

        JSONObject resultJson = api2Request.send("/v2/sv/deleteUser", userJson);
    }

}
